package top.yaofengqiao.springcloudsimple.demo.biz.infrastructure.repository;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Repository;
import top.yaofengqiao.springcloudsimple.demo.biz.domain.order.model.entity.OrderEntity;
import top.yaofengqiao.springcloudsimple.common.constant.RechargeOrderStatus;
import top.yaofengqiao.springcloudsimple.demo.biz.domain.order.repository.IRechargeOrderRepository;
import top.yaofengqiao.springcloudsimple.demo.biz.infrastructure.dao.RechargeOrderMapper;
import top.yaofengqiao.springcloudsimple.demo.biz.infrastructure.po.RechargeOrderPO;

import javax.annotation.Resource;

/**
 * @author yfq
 * @date 2024/6/14 11:33
 * @description
 */
@Repository
public class RechargeOrderRepository implements IRechargeOrderRepository {
    @Resource
    private RechargeOrderMapper rechargeOrderMapper;

    @Override
    public long save(OrderEntity order) {
        RechargeOrderPO orderPO = buildRechargeOrderPO(order);

        if (orderPO == null) return -1;

        int insert = rechargeOrderMapper.insert(orderPO);

        return insert == 1 ? orderPO.getId() : -1;
    }

    @Override
    public OrderEntity queryUnpaidOrder(String productId, long userId) {
        RechargeOrderPO orderPO = rechargeOrderMapper.selectOne(Wrappers.<RechargeOrderPO>lambdaQuery()
                .eq(RechargeOrderPO::getProductCode, productId)
                .eq(RechargeOrderPO::getUserId, userId)
                .eq(RechargeOrderPO::getOrderStatus, RechargeOrderStatus.CREATE.getStatus()));

        return buildOrderEntity(orderPO);
    }

    @Override
    public OrderEntity queryByOrderNo(String orderNo) {
        if (StrUtil.isEmpty(orderNo)) return null;

        RechargeOrderPO orderPO = rechargeOrderMapper.selectOne(Wrappers.<RechargeOrderPO>lambdaQuery().eq(RechargeOrderPO::getRechargeOrderNo, orderNo));

        return buildOrderEntity(orderPO);
    }

    @Override
    public OrderEntity queryByOrderId(long orderId) {
        RechargeOrderPO orderPO = rechargeOrderMapper.selectById(orderId);

        return buildOrderEntity(orderPO);
    }

    @Override
    public boolean updateRechargeOrderByOrderNo(OrderEntity orderEntity, String orderNo) {
        RechargeOrderPO rechargeOrderPO = buildRechargeOrderPO(orderEntity);
        if (rechargeOrderPO == null) return false;

        int updated = rechargeOrderMapper.update(rechargeOrderPO, Wrappers.<RechargeOrderPO>lambdaUpdate().eq(RechargeOrderPO::getRechargeOrderNo, orderNo));
        return updated == 1;
    }

    @Override
    public boolean changeToPayment(OrderEntity orderEntity) {
        return rechargeOrderMapper.changeToPayment(orderEntity) == 1;
    }

    @Override
    public void changeStatus(String rechargeOrderNo, RechargeOrderStatus origin, RechargeOrderStatus target) {
        rechargeOrderMapper.changeStatus(rechargeOrderNo, origin.getStatus(), target.getStatus());
    }

    private RechargeOrderPO buildRechargeOrderPO(OrderEntity order) {
        if (order == null) return null;

        RechargeOrderPO orderPO = new RechargeOrderPO();
        orderPO.setId(order.getId());
        orderPO.setRechargeOrderNo(order.getRechargeOrderNo());
        orderPO.setDeliverOrderNo(order.getDeliverOrderNo());
        orderPO.setUserId(order.getUserId());
        orderPO.setTitle(order.getTitle());
        orderPO.setProductType(order.getProductType());
        orderPO.setProductCode(order.getProductCode());
        orderPO.setOriginPrice(order.getOriginPrice());
        orderPO.setOrderAmount(order.getOrderAmount());
        orderPO.setPayAmount(order.getPayAmount());
        orderPO.setOrderStatus(order.getOrderStatus());
        orderPO.setRefundStatus(order.getRefundStatus());
        orderPO.setPayTime(order.getPayTime());
        orderPO.setCancelTime(order.getCancelTime());
        orderPO.setFinishTime(order.getFinishTime());
        orderPO.setFlag(order.getFlag());
        orderPO.setRemark(order.getRemark());
        orderPO.setCreateTime(order.getCreateTime());
        orderPO.setUpdateTime(order.getUpdateTime());

        return orderPO;
    }

    private OrderEntity buildOrderEntity(RechargeOrderPO orderPO) {
        if (orderPO == null) return null;

        return OrderEntity.builder()
                .id(orderPO.getId())
                .rechargeOrderNo(orderPO.getRechargeOrderNo())
                .deliverOrderNo(orderPO.getDeliverOrderNo())
                .userId(orderPO.getUserId())
                .title(orderPO.getTitle())
                .productType(orderPO.getProductType())
                .productCode(orderPO.getProductCode())
                .originPrice(orderPO.getOriginPrice())
                .orderAmount(orderPO.getOrderAmount())
                .payAmount(orderPO.getPayAmount())
                .orderStatus(orderPO.getOrderStatus())
                .refundStatus(orderPO.getRefundStatus())
                .payTime(orderPO.getPayTime())
                .cancelTime(orderPO.getCancelTime())
                .finishTime(orderPO.getFinishTime())
                .flag(orderPO.getFlag())
                .remark(orderPO.getRemark())
                .createTime(orderPO.getCreateTime())
                .updateTime(orderPO.getUpdateTime())
                .build();
    }
}
